<!--
 * @Description: 工单列表
 * @Author: charles
 * @Date: 2021-12-14 22:07:55
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-12-22 17:15:08
-->
<template>
  <div>
    <el-form ref="form" :rules="rules" :model="sizeForm" label-width="80px" size="mini" :inline="true">
      <el-form-item>
        <el-date-picker
          v-model="pickerDateTime"
          type="datetimerange"
          value-format="timestamp"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-select v-model="params.type" clearable placeholder="请选择工单类型">
          <el-option label="安装" value="安装"></el-option>
          <el-option label="维修" value="维修"></el-option>
          <el-option label="拆机" value="拆机"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="params.status" clearable placeholder="请选择工单状态">
          <el-option label="待接单" value="待接单"></el-option>
          <el-option label="已完成" value="已完成"></el-option>
          <el-option label="进行中" value="进行中"></el-option>
        </el-select>
      </el-form-item>
  <el-form-item size="medium">
    <el-button type="primary" @click="onSubmit">查询</el-button>
  </el-form-item>
    </el-form>
<!--表格-->
     <div class="table">
      <el-table :data="tableData.list" style="width: 100%" size="small">
          <el-table-column label="序号" type="index" width="50" align="center">
        </el-table-column>
      <el-table-column prop="engineer_name" label="工程名称" width="180"> </el-table-column>
      <el-table-column prop="device_name" label="设备名称"> </el-table-column>
        <el-table-column  prop="account_name" label="维修工"> </el-table-column>
        <el-table-column prop="bill_why" label="报修原因"> </el-table-column>
        <el-table-column prop="finish_note" label="完成记录">
      </el-table-column>
      <el-table-column label="下单时间">
        <template v-slot="scope">
          {{ scope.row.create_time | datefmt}}
        </template>
      </el-table-column>
      <!--
      <el-table-column
        prop="type"
        label="类型">
      </el-table-column>
   
      <el-table-column
        prop="status"
        label="状态">
      </el-table-column>
      -->
      <el-table-column prop="type" label="类型"   width="80" align="center">
          <template v-slot="scope">
                <el-tag v-if="scope.row.type == '安装'" size="mini" >{{ scope.row.type }}</el-tag>
                <el-tag v-else-if="scope.row.type == '维修'" type="info" size="mini" >{{ scope.row.type }}</el-tag>
                 <el-tag v-else type="danger" size="mini" >{{ scope.row.type }}</el-tag>
          </template>       
        </el-table-column>
        <el-table-column prop="status" label="状态"   width="80" align="center">
          <template v-slot="scope">
                <el-tag v-if="scope.row.status == '待接单'" type="warning" size="mini" >{{ scope.row.status }}</el-tag>
                <el-tag v-else-if="scope.row.status == '进行中'" size="mini" >{{ scope.row.status }}</el-tag>
          </template>       
        </el-table-column>
       <el-table-column label="操作" width="100" align="center">
          <template v-slot="scope">
            <el-button type="text" @click="detailsHandler(scope.row)"
              >详情</el-button
            >
            <el-button type="text" @click="cancelHandler(scope.row.id)"
              >取消</el-button
            >
          </template>
        </el-table-column>
    </el-table>
  </div>
   <!-- 分页组件 -->
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="parseInt(tableData.page)"
      :page-size="parseInt(tableData.pageSize)"
      layout="total, sizes, prev, pager, next"
      :total="tableData.total"
      :page-sizes="[5, 10, 15, 20]">
    </el-pagination>
  </div>
</template>

<script>
import { get, postJSON } from "@/utils/request";
export default {
  data (){
  return {
    sizeForm:{
      data1:'',
      data2:'',
      ordertype:'',
      orderstatus:'',
      delivery: false,
          type: [],
          resource: '',
          desc: ''
    },
    tableData: {},
     params: {
        page: 1,
        pageSize: 10,
      },
      rules :{
        engineer_id: [{required: true, message:'请输入工程编号'}],
        device_id: [{required: true, message:'请输入设备编号'}],
        account_id: [{required: true, message:'请输入报修工'}],
        bill_why: [{required: true, message:'请输入维修原因'}],    
      },
      pickerDateTime: []
  }
  },
  methods:{
   onSubmit(){
     if(this.pickerDateTime.length == 2) {
       this.params.begin_time = this.pickerDateTime[0]
       this.params.end_time = this.pickerDateTime[1]
     }
    this.pageQueryworkorder()
  },
  detailsHandler(row){
    this.$router.push({
      path: "/am/order/Details",
      query: row,
    });
      
  },
  cancelHandler(id){
       this.$confirm('此操作将取消订单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        get('/workorder/cancelOrder', { id }).then((res) => {
          this.pageQueryworkorder()
          this.$message.success('操作成功')
        })
      })
      
  },
  pageQueryworkorder() {
    // 使用axios封装的get方法，发送http请求
    get("/workorder/pageQuery", this.params).then((res) => {
      // 将后台接口返回的结果，赋值给tableData
      this.tableData = res.data;
    });  
  },
  handleSizeChange(pageSize){
  this.params.pageSize = pageSize
   this.pageQueryworkorder();
  },
  handleCurrentChange(page){
  this.params.page = page;
  this.pageQueryworkorder();
  },
   loadWorkorders(){
      let url = "/workorder/pageQuery";
      get(url,this.params).then(resp => {
        this.tableDataData = resp.data;
      })
    },
  },
  created(){
  this.pageQueryworkorder();
  }

}
</script>